package ${basePackage}.cli.command;


import cn.hutool.core.bean.BeanUtil;
import ${basePackage}.generator.MainGenerator;
import ${basePackage}.model.DataModel;
import lombok.Data;
import picocli.CommandLine.Option;
import picocli.CommandLine;
import java.util.concurrent.Callable;

<#-- 生成选项 -->
<#macro generateModels indent modelInfo>
    ${indent}@Option(names = {<#if modelInfo.abbr??>"-${modelInfo.abbr}", </#if>"--${modelInfo.fieldName}"}, arity = "0..1", <#if modelInfo.description??>description = "${modelInfo.description}", </#if>interactive = true, echo = true)
    ${indent}private ${modelInfo.type} ${modelInfo.fieldName}<#if modelInfo.defaultValue??> = ${modelInfo.defaultValue?c}</#if>;
</#macro>
<#--生成命令的宏调用-->
<#macro generateCommand indent modelInfo>
${indent}System.out.println("输入${modelInfo.groupName}配置：");
${indent}CommandLine ${modelInfo.groupKey}commandLine = new CommandLine(${modelInfo.type}Command.class);
${indent}${modelInfo.groupKey}commandLine.execute(${modelInfo.allArgsStr});
</#macro>
@CommandLine.Command(name = "generate", description = "生成代码", mixinStandardHelpOptions = true)
@Data
public class GenerateCommand implements Callable {



    <#list modelConfig.models as modelInfo>
        <#--    有分组-->
        <#if modelInfo.groupKey??>
        /**
        * ${modelInfo.description}
        */
        static DataModel.${modelInfo.type} ${modelInfo.groupKey} = new DataModel.${modelInfo.type}();
        <#--        根据分组生成命令类    -->
        @CommandLine.Command(name = "${modelInfo.groupKey}", description = "${modelInfo.description}",mixinStandardHelpOptions = true)
        @Data
        public static class ${modelInfo.type}Command implements Runnable{
        <#list modelInfo.models as model>
            <@generateModels indent="        " modelInfo=model/>
        </#list>
            @Override
            public void run() {
                <#list modelInfo.models as subModel>
                ${modelInfo.groupKey}.${subModel.fieldName} = ${subModel.fieldName};
                </#list>
            }
        }
        <#--        没有分组-->
        <#else >
        <@generateModels indent="    " modelInfo=modelInfo/>
        </#if>
    </#list>
<#--    生成调用方法-->
    @Override
    public Object call() throws Exception {
        <#list modelConfig.models as modelInfo>
        <#if modelInfo.groupKey??>
        <#if modelInfo.condition??>
            if(${modelInfo.condition}){
                <@generateCommand indent="            " modelInfo=modelInfo/>
            }
        <#else>
        <@generateCommand indent="      " modelInfo=modelInfo/>
        </#if>
        </#if>
        </#list>
<#--        填充数据模型对象-->
        DataModel dataModel = new DataModel();
        BeanUtil.copyProperties(this,dataModel);
        <#list modelConfig.models as modelInfo>
        <#if modelInfo.groupKey??>
        dataModel.${modelInfo.groupKey} = ${modelInfo.groupKey};
        </#if>
        </#list>
        MainGenerator.doGenerator(dataModel);
        return null;
    }
}
